//
//  HJCRefreshControl.swift
//  新浪微博Swift
//
//  Created by 郝俊聪 on 15/5/20.
//  Copyright (c) 2015年 heima. All rights reserved.
//

import UIKit

class HJCRefreshControl: UIRefreshControl {
    //定义一个标记。记录是否下拉
    var isLettingGOFlag = false
    //加一个正在加载的标记
    var isLoadingFlag = false
    
    var refreshView : HJCRefreshView?
    ///重写init。设置刷新图片
    override init() {
        super.init()
        creatRefreshViewFromXib()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    ///从xib加载时候
    override func awakeFromNib() {
        super.awakeFromNib()
        creatRefreshViewFromXib()
    }
    
    ///创建刷新视图
    func creatRefreshViewFromXib(){
        let refreshView = NSBundle.mainBundle().loadNibNamed("RefreshControl", owner: nil
            , options: nil).last as! HJCRefreshView
        self.refreshView = refreshView
        //加入refreshControl中
        addSubview(refreshView)
        
        //设置布局
        //禁止aoturesizing
        refreshView.setTranslatesAutoresizingMaskIntoConstraints(false)
        var cons = [AnyObject]()
        let marginH =  (UIScreen.mainScreen().bounds.size.width - refreshView.frame.size.width)*0.5
        //横向布局
        cons += NSLayoutConstraint.constraintsWithVisualFormat("H:[refreshView(150)]-margin-|", options: NSLayoutFormatOptions(0), metrics: ["margin":marginH], views: ["refreshView":refreshView])
        //纵向布局
        cons += NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[refreshView(60)]", options: NSLayoutFormatOptions(0), metrics: nil, views: ["refreshView":refreshView])
        
        //设置kvo监听frame
        addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions.New, context: nil)
        
        //将布局添加到父view
        addConstraints(cons)
        
    }
    //监听方法
    override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
       
        //获的刷新控件的y值
        
        let y = object.frame.origin.y
        
        if y > 0 {
           
           return
        }
        
        //开始刷新的时候
        if refreshing && !isLoadingFlag{
            //设置已经开始加载
            isLoadingFlag = true
            refreshView?.startLoadingAnimation()
            return
        }
  
        //展示下拉刷新
        if y >= -refreshView!.frame.size.height && isLettingGOFlag{
           
            isLettingGOFlag = false
            setPulling()
            
      }
        //展示释放更新
        if y < -refreshView!.frame.size.height && !isLettingGOFlag {
        
            //设置已经下拉到指定位置
           isLettingGOFlag = true
           setLettiongGo()
           
        }
        
        
        
        
    }
    
    //移除kvo
    deinit{
       removeObserver(self, forKeyPath: "frame")
        
    }
    
    //释放更新操作
    func setLettiongGo(){
       
//        println("释放")
        refreshView?.rotatePullImageView(true)

    }
    
    //下拉刷新操作
    func setPulling(){
        
//        println("下拉")
        refreshView?.rotatePullImageView(false)
        
    }
    
    //重写结束刷新
    override func endRefreshing() {
        super.endRefreshing()
        //结束动画
        refreshView?.endLoadingAnimation()
        //设置刷新标记
        isLoadingFlag = false
    }

}
